﻿Imports System.ComponentModel
Imports DevExpress.Skins
Imports DevExpress.LookAndFeel
Imports DevExpress.UserSkins
Imports DevExpress.XtraBars
Imports DevExpress.XtraBars.Ribbon
Imports DevExpress.XtraBars.Helpers
Imports DevExpress.XtraSplashScreen
Imports DevExpress.XtraEditors
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Grid

Public Class BH_HopDongMuaBan
    Dim Ma As String
    Dim State As Integer = 0 'trạng thái form '0 mặc định'1 tạo mới'2 sửa '3 xoa
    Dim DoanhThu, PhanTramThue, TienThue, TongCong As Decimal
    Dim DienGiaiThue, MaDonVi, TenDonVi, DiaChi As String
    Dim TaiKhoanNo, TaiKhoanCo, TaiKhoanThue As String
    Dim MaKhachHang, TenKhachHang, DiaChiKhachHang As String
    Dim flagTyGia As Boolean
    Private flag As Boolean = False 'tránh loop xtragrid
    Private Sub setReadonlyControl(ByVal b As Boolean)
        setReadonly(Me.layoutControl, b)
        If b Then
            Me.IGridViewHangHoa.OptionsBehavior.Editable = False
            Me.IGridViewDoiTuongApDung.OptionsBehavior.Editable = False
            Me.IGridViewHangHoa.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None
            Me.IGridViewDoiTuongApDung.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None
        Else
            Me.IGridViewHangHoa.OptionsBehavior.Editable = True
            Me.IGridViewDoiTuongApDung.OptionsBehavior.Editable = True
            Me.IGridViewHangHoa.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom
            Me.IGridViewDoiTuongApDung.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom
        End If
        If b Then
            'dxprovidervalidation
            Me.DxValidationProvider1.ValidationMode = DXErrorProvider.ValidationMode.Manual
            setButtonTrangThaiSua(False)
        Else
            Me.DxValidationProvider1.ValidationMode = DXErrorProvider.ValidationMode.Auto
            setButtonTrangThaiSua(True)
        End If
    End Sub
    Private Sub setButtonTrangThaiSua(ByVal b As Boolean) 'set nút lưu và xóa - khi ở trạng thái sửa thì mới cho phép thao tác trên nó (enable = true) - các trạng thái khác thì không cho (enable = false)
        Me.BarButtonItemLuu.Enabled = b
        Me.BarButtonItemXoa.Enabled = b
        If b Then 'xử lý nút sửa (nếu nút lưu - xóa cho phép thì sửa ko cho phép và ngược lại)
            Me.BarButtonItemSua.Enabled = False
        Else
            Me.BarButtonItemSua.Enabled = True
        End If
        setButtonChiTietTrangThaiSua(b)
    End Sub
    Private Sub setButtonChiTietTrangThaiSua(ByVal b As Boolean)
        Me.BarButtonItemTaoMoiChiTiet.Enabled = b
        Me.BarButtonItemXoaChiTiet.Enabled = b
        Me.BarButtonItemHuyThayDoiChiTiet.Enabled = b
        Me.BarButtonItemChuyenDuLieuThue.Enabled = b
    End Sub
    Private Sub loadDuLieu()
        SplashScreenManager.ShowForm(GetType(WaitForm1))
        Me.DMKichCoTableAdapter.Fill(Me.PCV_FISHDataSet.DMKichCo)
        Me.Dmhanghoa_donvitinhTableAdapter.Fill(Me.PCV_FISHDataSet.dmhanghoa_donvitinh)
        Me.DmhanghoaTableAdapter.FillByMaTenDonViTinh(Me.PCV_FISHDataSet.dmhanghoa)
        Me.GetDMNhanVien_NhanVienBanHangTableAdapter.Fill(Me.PCV_FISHDataSet.getDMNhanVien_NhanVienBanHang)
        Me.GetPhanLoai_ThanhToanTableAdapter.Fill(Me.PCV_FISHDataSet.getPhanLoai_ThanhToan)
        Me.GetPhanLoai_TienTeTableAdapter.Fill(Me.PCV_FISHDataSet.getPhanLoai_TienTe)
        Me.GetPhanLoai_TinhTrangPhieuTableAdapter.Fill(Me.PCV_FISHDataSet.getPhanLoai_TinhTrangPhieu)
        Me.DMTaiKhoanKeToanCap2TableAdapter.Fill(Me.PCV_FISHDataSet.DMTaiKhoanKeToanCap2)
        Me.GetDMTyGia_TyGiaTableAdapter.Fill(Me.PCV_FISHDataSet.getDMTyGia_TyGia, Today)
        Me.DmtygiaTableAdapter.Fill(Me.PCV_FISHDataSet.dmtygia)
        Me.DmkhachhangTableAdapter.Fill(Me.PCV_FISHDataSet.dmkhachhang)
        Me.BHHopDongMuaBanDMHangTableAdapter.Fill(Me.PCV_FISHDataSet.BHHopDongMuaBanDMHang)
        Me.BHHopDongMuaBanTableAdapter.Fill(Me.PCV_FISHDataSet.BHHopDongMuaBan)
        setReadonlyControl(True)
        SplashScreenManager.CloseForm()
    End Sub
    Private Sub BH_BanHangNuocNgoai_Activated(sender As Object, e As System.EventArgs) Handles Me.Activated
        If Me.flagTyGia Then
            Me.DmtygiaTableAdapter.Fill(Me.PCV_FISHDataSet.dmtygia)
        End If
    End Sub
    Private Sub KH_HangHoa_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.loadDuLieu()
        Me.IGridViewHangHoa.setBatLoi("taikhoanno", "not null")
        Me.IGridViewHangHoa.setBatLoi("taikhoanco", "not null")
        Me.IGridViewHangHoa.setBatLoi("mahang", "not null")
        Me.IGridViewHangHoa.setBatLoi("solo", "not null")
        Me.IGridViewHangHoa.setBatLoi("hsd", "not null")
        Me.IGridViewHangHoa.setBatLoi("taikhoanthueNK", "not null")
        Me.IGridViewHangHoa.setBatLoi("taikhoanchietkhau", "not null")
        Me.IGridViewHangHoa.setBatLoi("soluonggiao", "not 0 and not null")
        Me.IGridViewHangHoa.setBatLoi("soluongSO", "not 0 and not null")
    End Sub

    Private Sub BarButtonItemTaoMoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemTaoMoi.ItemClick
        'focus
        Me.setReadonlyControl(False)
        Me.MahopdongITextEdit.Focus()
        'Mã //set ngầm
        Dim row As DataRowView = CType(BHHopDongMuaBanBindingSource.AddNew(), DataRowView)
        Me.Ma = "BHBG" & System.Guid.NewGuid.ToString()
        row("ma") = Ma
        'mặc định
        Me.NgayhopdongIDateEdit.EditValue = Today
        Me.TinhtrangISearchLookupEdit.EditValue = "NL"
        Me.State = 1
    End Sub

    Private Sub BarButtonItemLuu_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLuu.ItemClick
        Try
            Me.IGridViewHangHoa.PostEditor()
            Me.IGridViewHangHoa.UpdateCurrentRow()
            Me.IGridViewHangHoa.CloseEditor()
            Me.IGridViewDoiTuongApDung.PostEditor()
            Me.IGridViewDoiTuongApDung.UpdateCurrentRow()
            Me.IGridViewDoiTuongApDung.CloseEditor()
            Me.BHHopDongMuaBanBindingSource.EndEdit() 'end edit trước khi lưu - ghi những thay đổi 
            Me.FKBHHopDongMuaBanDMHangBHHopDongMuaBanBindingSource.EndEdit()
            If Me.PCV_FISHDataSet.HasChanges Then 'coi có thay đổi dữ liệu hay ko ?
                If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn 'Lưu' không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                    Try
                        Me.TableAdapterManager.UpdateAll(Me.PCV_FISHDataSet)
                        Me.State = 0 'trở về trạng thái mặc định 0
                        Me.setReadonlyControl(True) 'set lại readonly
                        setButtonTrangThaiSua(False) 'set lại nút 
                    Catch ex As Exception
                        DevExpress.XtraEditors.XtraMessageBox.Show("Xảy ra lỗi :" & ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End Try
                End If
            Else 'nếu ko thay đổi dữ liệu
                DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu ?", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Me.setReadonlyControl(True) 'set lại readonly
                setButtonTrangThaiSua(False) 'set lại nút 
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message) 'tung message khi có ngoại lệ
        End Try
    End Sub

    Private Sub BarButtonItemSua_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemSua.ItemClick
        Me.setReadonlyControl(False)
        Me.State = 2
    End Sub

    Private Sub IGridView1_CellValueChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs)
        Dim view1 As GridView = CType(sender, GridView)
        Dim dongianguyente, dongiavnd, thanhtiennguyente, thanhtienvnd, tienthueGTGT As Decimal
        If (e.Column.FieldName = "soluonggiao" OrElse e.Column.FieldName = "dongiavnd" OrElse e.Column.FieldName = "dongianguyente" OrElse e.Column.FieldName = "tilethueGTGT") AndAlso (Not flag) AndAlso IsDBNull(e.Value) = False Then
            Try
                dongianguyente = CDec(view1.GetRowCellValue(e.RowHandle, view1.Columns("dongianguyente")))
                'dongiavnd = dongianguyente * CDec(Me.TygianganhangITextEdit.EditValue)
                thanhtiennguyente = CDec(Me.IGridViewHangHoa.GetFocusedRowCellValue("soluonggiao")) * dongianguyente
                thanhtienvnd = CDec(Me.IGridViewHangHoa.GetFocusedRowCellValue("soluonggiao")) * dongiavnd
                tienthueGTGT = thanhtienvnd * CDec(Me.IGridViewHangHoa.GetFocusedRowCellValue("tilethueGTGT")) / 100
            Catch ex As Exception
                Exit Sub
            End Try
            flag = True
            view1.SetRowCellValue(e.RowHandle, view1.Columns("dongiavnd"), dongiavnd)
            view1.SetRowCellValue(e.RowHandle, view1.Columns("thanhtiennguyente"), thanhtiennguyente)
            view1.SetRowCellValue(e.RowHandle, view1.Columns("thanhtienvnd"), thanhtienvnd)
            view1.SetRowCellValue(e.RowHandle, view1.Columns("tienthueGTGT"), tienthueGTGT)
            flag = False
        End If
        'Me.NguyenteITextEdit.EditValue = CDec(Me.IGridViewHangHoa.Columns("thanhtiennguyente").SummaryItem.SummaryValue)
        'Me.ThanhtienvndITextEdit.EditValue = CDec(Me.IGridViewHangHoa.Columns("thanhtienvnd").SummaryItem.SummaryValue)
        Me.IGridViewHangHoa.UpdateCurrentRow()
        Me.IGridViewHangHoa.UpdateSummary()
    End Sub

    Private Sub IGridViewHangHoa_FocusedRowChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs)
        Dim currentView As GridView = CType(sender, GridView)
        Dim groupRowHandle As Integer
        If (e.FocusedRowHandle >= 0) Then
            groupRowHandle = currentView.GetParentRowHandle(e.FocusedRowHandle)
        Else
            groupRowHandle = e.FocusedRowHandle
        End If
        If Not currentView.IsGroupRow(groupRowHandle) Then Return
        Dim ht As Hashtable = currentView.GetGroupSummaryValues(groupRowHandle)
        Dim entry As DictionaryEntry
        For Each entry In ht
            Dim sumItem As GridGroupSummaryItem = entry.Key
            Dim val As Object = entry.Value
            Try
                If Me.IGridViewHangHoa.GetGroupRowValue(Me.IGridViewHangHoa.FocusedRowHandle) = "133" Then
                    Me.TienThue += CDec(val)
                    Me.DienGiaiThue = Me.IGridViewHangHoa.GetGroupRowValue(Me.IGridViewHangHoa.FocusedRowHandle, Me.IGridViewHangHoa.Columns("tenhang"))
                Else
                    Me.DoanhThu += CDec(val)
                End If
            Catch ex As Exception
            End Try
        Next
    End Sub

    Private Sub BarButtonItemThoat_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemThoat.ItemClick
        Me.Dispose() 'giải phóng ram
        Me.Close() 'đóng
    End Sub

    Private Sub BarButtonItemTaoMoiChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemTaoMoiChiTiet.ItemClick
        Me.IGridViewHangHoa.MoveLast()
        Me.IGridViewHangHoa.Focus()
        Me.IGridViewHangHoa.AddNewRow()
        Me.IGridViewHangHoa.FocusedColumn = Me.IGridViewHangHoa.VisibleColumns(0)
        Me.IGridViewHangHoa.ShowEditor()
    End Sub

    Private Sub RepositoryItemSearchLookUpEdit5_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs)
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowHandle As Integer = editor.Properties.GetIndexByKeyValue(e.NewValue)
        IGridViewHangHoa.SetFocusedRowCellValue("mahang", editor.Properties.View.GetRowCellValue(rowHandle, "ma"))
        IGridViewHangHoa.SetFocusedRowCellValue("tenhang", editor.Properties.View.GetRowCellValue(rowHandle, "ten1"))
        IGridViewHangHoa.SetFocusedRowCellValue("dvt", editor.Properties.View.GetRowCellValue(rowHandle, "donvitinh"))
    End Sub
    Private Sub BarButtonItemFirst_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemFirst.ItemClick
        Me.BHHopDongMuaBanBindingSource.MoveFirst()
    End Sub

    Private Sub BarButtonPrevious_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonPrevious.ItemClick
        Me.BHHopDongMuaBanBindingSource.MovePrevious()
    End Sub

    Private Sub BarButtonNext_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonNext.ItemClick
        Me.BHHopDongMuaBanBindingSource.MoveNext()
    End Sub

    Private Sub BarButtonLast_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonLast.ItemClick
        Me.BHHopDongMuaBanBindingSource.MoveLast()
    End Sub

    Private Sub BarButtonItemFirstChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemFirstChiTiet.ItemClick
        If Me.TabbedControlGroup2.SelectedTabPageIndex = 0 Then
            Me.FKBHHopDongMuaBanDMHangBHHopDongMuaBanBindingSource.MoveFirst()
        End If
    End Sub

    Private Sub BarButtonItemPreviousChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemPreviousChiTiet.ItemClick
        If Me.TabbedControlGroup2.SelectedTabPageIndex = 0 Then
            Me.FKBHHopDongMuaBanDMHangBHHopDongMuaBanBindingSource.MovePrevious()
        End If
    End Sub

    Private Sub BarButtonItemNextChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemNextChiTiet.ItemClick
        If Me.TabbedControlGroup2.SelectedTabPageIndex = 0 Then
            Me.FKBHHopDongMuaBanDMHangBHHopDongMuaBanBindingSource.MoveNext()
        End If
    End Sub

    Private Sub BarButtonItemLastChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLastChiTiet.ItemClick
        If Me.TabbedControlGroup2.SelectedTabPageIndex = 0 Then
            Me.FKBHHopDongMuaBanDMHangBHHopDongMuaBanBindingSource.MoveLast()
        End If
    End Sub

    Private Sub BarButtonItemXoa_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemXoa.ItemClick
        If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn xóa hợp đồng số '" & Me.MahopdongITextEdit.EditValue & "' không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
            Me.BHHopDongMuaBanBindingSource.RemoveCurrent()
            Me.BHHopDongMuaBanTableAdapter.Update(Me.PCV_FISHDataSet.BHHopDongMuaBan)
            Me.setReadonlyControl(True)
            Me.State = 0 'trở về mặc định
        End If
    End Sub

    Private Sub BarButtonItemHuyThayDoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemHuyThayDoi.ItemClick
        Me.BHHopDongMuaBanBindingSource.EndEdit()
        If Me.PCV_FISHDataSet.HasChanges Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn hủy tất cả những thay đổi đang làm không?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.PCV_FISHDataSet.RejectChanges()
            End If
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub BarButtonItemXoaChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemXoaChiTiet.ItemClick
        If Me.TabbedControlGroup2.SelectedTabPageIndex = 0 Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn xóa '" & Me.IGridViewHangHoa.GetRowCellValue(Me.IGridViewHangHoa.FocusedRowHandle, "mahang") & "' không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.IGridViewHangHoa.DeleteRow(Me.IGridViewHangHoa.FocusedRowHandle)
                Me.IGridViewHangHoa.UpdateCurrentRow()
                Me.IGridViewHangHoa.UpdateGroupSummary()
                Me.IGridViewHangHoa.UpdateSummary()
            End If
        End If
        If Me.TabbedControlGroup2.SelectedTabPageIndex = 2 Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn xóa '" & Me.IGridViewDoiTuongApDung.GetRowCellValue(Me.IGridViewDoiTuongApDung.FocusedRowHandle, "mahang") & "' không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.IGridViewDoiTuongApDung.DeleteRow(Me.IGridViewDoiTuongApDung.FocusedRowHandle)
                Me.IGridViewDoiTuongApDung.UpdateCurrentRow()
                Me.IGridViewDoiTuongApDung.UpdateGroupSummary()
                Me.IGridViewDoiTuongApDung.UpdateSummary()
            End If
        End If
    End Sub

    Private Sub BarButtonItemHuyThayDoiChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemHuyThayDoiChiTiet.ItemClick
        Dim tabname As String
        If Me.TabbedControlGroup2.SelectedTabPageIndex = 0 Then
            Me.FKBHHopDongMuaBanDMHangBHHopDongMuaBanBindingSource.EndEdit()
            tabname = "'Danh mục hàng'"
        End If
        If Me.PCV_FISHDataSet.HasChanges Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn hủy tất cả những thay đổi của phần " & tabname & " không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.PCV_FISHDataSet.RejectChanges()
            End If
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub BarButtonItemLamTuoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLamTuoi.ItemClick
        Dim position As Integer = Me.BHHopDongMuaBanBindingSource.Position 'load dữ liệu lại nhưng giữ nguyên phiếu
        loadDuLieu()
        Me.BHHopDongMuaBanBindingSource.Position = position
    End Sub

    Private Sub BarButtonItem5_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemChuyenDuLieuThue.ItemClick
        'If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn chuyển dữ liệu thuế tự động không ?'", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
        '    Me.IGridViewHangHoa.Columns("taikhoanno").GroupIndex = 0
        '    Me.IGridViewHangHoa.Columns("sohoadon").GroupIndex = 1
        'For index = 0 To Me.IGridViewHangHoa.RowCount - 1
        '    Me.IGridViewHangHoa.MoveNext()
        'Next
        '    Me.IGridViewHangHoa.ClearGrouping()
        '    Me.TabbedControlGroup2.SelectedTabPage = Me.TabbedControlGroup2.TabPages(2) 'chọn tab thue
        '    Me.IGridViewThue.AddNewRow()
        'End If
        MessageBox.Show(Me.IGridViewKhachHang.RowCount)
    End Sub


    Private Sub LoaitienISearchLookupEdit_AddNewValue1(sender As Object, e As DevExpress.XtraEditors.Controls.AddNewValueEventArgs)
        DM_TyGia.Show()
        DM_TyGia.MdiParent = Me.MdiParent
        DM_TyGia.WindowState = FormWindowState.Maximized
        Me.flagTyGia = True
    End Sub

    Private Sub IGridViewHangHoa_CellValueChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles IGridViewHangHoa.CellValueChanged

    End Sub

    Private Sub IGridViewHangHoa_CellValueChanging(sender As Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles IGridViewHangHoa.CellValueChanging

    End Sub

    Private Sub IGridViewHangHoa_InitNewRow(sender As Object, e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewHangHoa.InitNewRow
        Me.IGridViewHangHoa.SetFocusedRowCellValue("uid", System.Guid.NewGuid().ToString)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("dongianguyente", 0)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("dongiavnd", 0)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("thanhtiennguyente", 0)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("thanhtienvnd", 0)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("soluong", 0)
    End Sub

    Private Sub ChọnTấtCảToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ChọnTấtCảToolStripMenuItem.Click
        For index = 0 To Me.IGridViewKhachHang.RowCount - 1
            Me.IGridViewKhachHang.SetRowCellValue(index, "ghichu", "Yes")
        Next

    End Sub

    Private Sub BỏTấtCảToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles BỏTấtCảToolStripMenuItem.Click
        For index = 0 To Me.IGridViewKhachHang.RowCount - 1
            Me.IGridViewKhachHang.SetRowCellValue(index, "ghichu", "No")
        Next
    End Sub

    Private Sub IGridViewDoiTuongApDung_InitNewRow(sender As Object, e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewDoiTuongApDung.InitNewRow
        Me.IGridViewDoiTuongApDung.SetFocusedRowCellValue("uid", System.Guid.NewGuid().ToString)
        Me.IGridViewDoiTuongApDung.SetFocusedRowCellValue("makhachhang", Me.MaKhachHang)
        Me.IGridViewDoiTuongApDung.SetFocusedRowCellValue("tenkhachhang", Me.TenKhachHang)
        Me.IGridViewDoiTuongApDung.SetFocusedRowCellValue("diachi", Me.DiaChiKhachHang)
    End Sub

    Private Sub BarButtonItem6_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem6.ItemClick
        If DevExpress.XtraEditors.XtraMessageBox.Show(Me.popupControlContainer1, "Bạn có muốn lấy các khách hàng đã chọn vào 'danh sách áp dụng' không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
            Me.IGridViewKhachHang.PostEditor()
            Me.IGridViewKhachHang.UpdateCurrentRow()
            Me.IGridViewKhachHang.CloseEditor()
            For index = 0 To Me.IGridViewKhachHang.RowCount - 1
                If Me.IGridViewKhachHang.GetRowCellValue(index, "ghichu").ToString = "Yes" Then
                    Me.MaKhachHang = Me.IGridViewKhachHang.GetRowCellValue(index, "ma")
                    Me.TenKhachHang = Me.IGridViewKhachHang.GetRowCellValue(index, "ten")
                    Try
                        Me.DiaChiKhachHang = Me.IGridViewKhachHang.GetRowCellValue(index, "diachi")
                    Catch ex As Exception
                        Me.DiaChiKhachHang = ""
                    End Try
                    Me.IGridViewDoiTuongApDung.AddNewRow()
                End If
            Next
        End If
    End Sub

    Private Sub BarButtonItem11_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem11.ItemClick
        Me.popupControlContainer1.HidePopup()
    End Sub

    Private Sub RepositoryItemSearchLookUpEdit_HangHoa_EditValueChanging(sender As Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles RepositoryItemSearchLookUpEdit_HangHoa.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowHandle As Integer = editor.Properties.GetIndexByKeyValue(e.NewValue)
        IGridViewHangHoa.SetFocusedRowCellValue("mahang", editor.Properties.View.GetRowCellValue(rowHandle, "ma"))
        IGridViewHangHoa.SetFocusedRowCellValue("tenhang", editor.Properties.View.GetRowCellValue(rowHandle, "ten1"))
        IGridViewHangHoa.SetFocusedRowCellValue("donvitinh", editor.Properties.View.GetRowCellValue(rowHandle, "donvitinh"))
    End Sub

    Private Sub TinhtrangISearchLookupEdit_KeyDown_1(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles TinhtrangISearchLookupEdit.KeyDown
        If State <> 0 And (e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Tab) Then
            Me.TabbedControlGroup2.SelectedTabPage = Me.TabbedControlGroup2.TabPages(0) 'chọn tab hàng hóa
            Me.BHHopDongMuaBanBindingSource.EndEdit()
            Me.IGridViewHangHoa.setFocusDongDauTien()
        End If
    End Sub

End Class
